from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, JSON, ForeignKey, Enum
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.core.database import Base
import enum


class NotificationType(str, enum.Enum):
    TEAM_INVITATION = "team_invitation"
    TEAM_ACCEPTED = "team_accepted"
    COMPETITION_REMINDER = "competition_reminder"
    PROJECT_COMMENT = "project_comment"
    SYSTEM_ANNOUNCEMENT = "system_announcement"
    ACHIEVEMENT = "achievement"


class Notification(Base):
    __tablename__ = "notifications"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 通知内容
    type = Column(Enum(NotificationType), nullable=False)
    title = Column(String(200), nullable=False)
    message = Column(Text, nullable=False)
    
    # 相关数据
    data = Column(JSON, default=dict)  # 额外的数据，如链接、ID等
    action_url = Column(String(500))  # 点击通知后跳转的URL
    
    # 状态
    is_read = Column(Boolean, default=False)
    is_sent = Column(Boolean, default=False)  # 是否已发送（邮件/推送）
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    read_at = Column(DateTime(timezone=True))
    
    # 关系
    user = relationship("User", back_populates="notifications")


class ChatRoom(Base):
    __tablename__ = "chat_rooms"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100))
    description = Column(Text)
    
    # 房间类型
    type = Column(String(20), default="team")  # team, competition, general
    
    # 关联ID
    team_id = Column(Integer, ForeignKey("teams.id"))
    competition_id = Column(Integer, ForeignKey("competitions.id"))
    
    # 设置
    is_private = Column(Boolean, default=False)
    max_members = Column(Integer, default=50)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    messages = relationship("ChatMessage", back_populates="room")
    members = relationship("ChatRoomMember", back_populates="room")


class ChatMessage(Base):
    __tablename__ = "chat_messages"

    id = Column(Integer, primary_key=True, index=True)
    room_id = Column(Integer, ForeignKey("chat_rooms.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 消息内容
    content = Column(Text, nullable=False)
    message_type = Column(String(20), default="text")  # text, image, file, system
    
    # 文件信息（如果是文件消息）
    file_url = Column(String(500))
    file_name = Column(String(255))
    file_size = Column(Integer)
    
    # 状态
    is_edited = Column(Boolean, default=False)
    is_deleted = Column(Boolean, default=False)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    room = relationship("ChatRoom", back_populates="messages")
    user = relationship("User")


class ChatRoomMember(Base):
    __tablename__ = "chat_room_members"

    id = Column(Integer, primary_key=True, index=True)
    room_id = Column(Integer, ForeignKey("chat_rooms.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 成员角色
    role = Column(String(20), default="member")  # admin, moderator, member
    
    # 状态
    is_muted = Column(Boolean, default=False)
    last_read_message_id = Column(Integer)
    
    # 时间戳
    joined_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    room = relationship("ChatRoom", back_populates="members")
    user = relationship("User")